﻿package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.Button;
	import fl.controls.TextArea;
	import com.adobe.flash.samples.MorseCode;
	
	/**
	* Defines the document class used by the DynamicSoundSample2 application. (In Flash CS4, you can set the
	* document class for a SWF application in the Publish Settings dialog box. (Select the File > Publish Settings command.
	* Then, in the Publish Settings dialog box, select the Flash tab and click the Settings button next to the Script drop-down list.)
	*/
	public class DynamicSoundSample2 extends MovieClip
	{
		public var playButton:Button;	// A Button component defined on the stage of the DynamicSoundSample2 FLA file.
		public var codeTextArea:TextArea;	// TextArray component defined in the stage of the DynamicSoundSample2 FLA file
		private var morse:MorseCode;	// The MorseCode class provides functionality for playing Morse Code audio.
		
		/**
		* Called when the SWF file initializes. This constructor function defines a click event handler for the 
		* playButton button. It also instantiates a MorseCode object.
		*/
		public function DynamicSoundSample2()
		{
			playButton.addEventListener(MouseEvent.CLICK, playMorse);
			morse = new MorseCode();
		}
		/**
		* Calls the playString() method of MorseCode object, passing the string from the user interface (the codeTextArea object).
		* The soundComplete event listener for the morse object defines what happens when the sound has completed playing.
		* The method also adjusts labels and event handlers for the playButton Button component.
		*/
		private function playMorse(event:MouseEvent):void
		{
			playButton.label = "Stop";
			playButton.removeEventListener(MouseEvent.CLICK, playMorse);
			playButton.addEventListener(MouseEvent.CLICK, stopMorse);
			
			morse.playString(codeTextArea.text);
			morse.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			
		}
		/**
		* Called when the user clicks the playButton Button (when the audio is playing). This method
		* stops the audio from playing.
		*/
		private function stopMorse(event:MouseEvent):void
		{
			morse.stop();
		}
		/**
		* Called when the audio stops. This method adjusts the label and click event handler for the 
		* playButton Button object.
		*/
		private function soundCompleteHandler(event:Event):void
		{
			playButton.label = "Play Morse Code";
			playButton.addEventListener(MouseEvent.CLICK, playMorse);
			playButton.removeEventListener(MouseEvent.CLICK, stopMorse)
		}
	}
}